'''
==============================
5、将识别的时间及二维码信息保存到数据库中
==============================
'''
# coding=utf-8
import cv2
import pyzbar.pyzbar as pyzbar
import datetime
import pymysql


def savedata(stu_id):
    db = pymysql.connect(host='127.0.0.1',
                         port=3306,
                         user='root',
                         password='wy123456',
                         db='code_recoad',
                         charset='utf8')
    cursor = db.cursor()
    now_time = datetime.datetime.now().strftime(
        "%Y-%m-%d %H:%M:%S")  # strftime格式化时间函数
    data = (stu_id, now_time)
    sql = """INSERT INTO recode2(stu_id,
         time)
         VALUES ('%s', '%s')"""
    try:
        cursor.execute(sql % data)
        db.commit()
    except:
        print("请勿重复打卡！")
        db.rollback()
    finally:
        str = "打卡成功！（已保存数据库）"
        print(stu_id + str)
        db.close()


def decodeDisplay(image):
    barcodes = pyzbar.decode(image)
    for barcode in barcodes:
        # 提取二维码的边界框的位置
        # 画出图像中条形码的边界框
        (x, y, w, h) = barcode.rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (225, 225, 225), 2)

        # 提取二维码数据为字节对象，所以如果我们想在输出图像上
        # 画出来，就需要先将它转换成字符串
        global barcodeData
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type

        # 绘出图像上条形码的数据和条形码类型
        text = "{} ({})".format(barcodeData, barcodeType)
        cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    .5, (225, 225, 225), 2)
        savedata(barcodeData)
        # 向终端打印条形码数据和条形码类型
        # print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
    return image


def detect():
    camera = cv2.VideoCapture(0)
    while True:
        # 读取当前帧
        ret, frame = camera.read()
        # 转为灰度图像
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        im = decodeDisplay(gray)

        cv2.waitKey(5)
        cv2.imshow("camera", im)
        # 如果按键q则跳出本次循环
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    camera.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    detect()
